package com.pks.resultful.common;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.pks.util.UtilString;
import com.pks.web.dto.ServiceResult;

import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

public class SRemoteResult<T> implements Serializable {
	
	private static final long serialVersionUID = 4256443394666341963L;
	
	private static final int S_SUCCESS = 1;
	private static final int S_ERROR = 0;
	 
	public static final Gson GSON_DT = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
	
	@ApiModelProperty(value = "返回标识 1-成功 0-失败", example = "1")
	private int s;
	@ApiModelProperty(value = "返回内容")
	private T d;
	@ApiModelProperty(value = "返回列表时的数据总数" , example = "0")
	private Integer total;
	@ApiModelProperty(value = "返回描述" , example = "success")
	private String msg;
	
	public static SRemoteResult<?> fromServiceResult(ServiceResult sr) {
		if (sr == null) {
			return null;
		}
		if (sr.success()) {
			return initSuccess(sr.getD());
		}
		SRemoteResult<String> result = new SRemoteResult<String>();
		result.setS(sr.getS());
		result.setMsg(String.valueOf(sr.getD()));
		return result;
	}
	
	public static <T> SRemoteResult<T> initPageList(T d , Integer total ){
		SRemoteResult<T> result = new SRemoteResult<T>();
		result.setS(S_SUCCESS);
		result.setTotal(total);
		result.setD(d);
		return result;
	}
	
	public static <T> SRemoteResult<T> initPageList(T d ){
		SRemoteResult<T> result = new SRemoteResult<T>();
		result.setS(S_SUCCESS);
		result.setD(d);
		return result;
	}
	
	/**
	 * 自定义返回状态和返回文字
	 * @param s
	 * @param msg
	 * @return
	 */
	public static <T> SRemoteResult<T> initCustom(Integer s,String msg) {
		return new SRemoteResult<T>(s, msg);
	}
	
	/***
	 * 执行成功
	 * @param d
	 * @return
	 */
	public static <T> SRemoteResult<T> initSuccess(T d) {
		return new SRemoteResult<T>(d);
	}

	/**
	 * 执行失败
	 * @param d
	 * @return
	 */
	public static <T> SRemoteResult<T> initError(String d) {
		return new SRemoteResult<T>(S_ERROR, d);
	}
	
	/**
	 * 根据状态码枚举返回
	 * @param rc
	 * @return
	 */
	public static <T> SRemoteResult<T> initErrorResultCode(SResultCode rc) {
		return new SRemoteResult<T>(rc.getCode(), rc.getMsg());
	}
	
	/**
	 * 根据状态码枚举返回
	 * @param rc
	 * @param paramName 参数名，拼接在msg 里面
	 * @return
	 */
	public static <T> SRemoteResult<T> initErrorResultCode(SResultCode rc,String paramName) {
		if (UtilString.isBlank(paramName)) {
			return initErrorResultCode(rc);
		} else {
			return new SRemoteResult<T>(rc.getCode(), String.format("[%s]%s", paramName,rc.getMsg()));
		}
	}

	/***
	 * 参数为空
	 * @param paramName 参数名 可为null
	 * @return
	 */
	public static <T> SRemoteResult<T> initErrorParamBlank(String paramName) {
		return initErrorResultCode(SResultCode.PARAM_IS_BLANK, paramName);
	}
	
	/***
	 * 参数无效
	 * @param paramName 参数名 可为null
	 * @return
	 */
	public static <T> SRemoteResult<T> initErrorParamInvalid(String paramName) {
		return initErrorResultCode(SResultCode.PARAM_IS_INVALID,paramName);
	}

	/***
	 * 参数缺失
	 * @param paramName 参数名 可为null
	 * @return
	 */
	public static <T> SRemoteResult<T> initErrorParamMissing(String paramName) {
		return initErrorResultCode(SResultCode.PARAM_MISSING, paramName);
	}
	
	/***
	 * 数据不存在
	 * @return
	 */
	public static <T> SRemoteResult<T> initErrorDataNone() {
		return initError("数据不存在");
	}
	
	/***
	 * 系统繁忙
	 * @return
	 */
	public static <T> SRemoteResult<T> initErrorSystem() {
		return initErrorResultCode(SResultCode.SYSTEM_INNER_ERROR);
	}

	/***
	 * 无权限访问
	 * @return
	 */
	public static <T> SRemoteResult<T> initErrorNoPerm() {
		return initErrorResultCode(SResultCode.PERM_UNACCESS);
	}
	
	public static String initErrorNoPermJson() {
		return GSON_DT.toJson(initErrorNoPerm());
	}

	/***
	 * 未登录
	 * @param d
	 * @return
	 */
	public static <T> SRemoteResult<T> initErrorLogin(String d) {
		return new SRemoteResult<T>(SResultCode.PERM_UNLOGIN.getCode(), UtilString.isNotBlank(d) ? d : "请重新登录");
	}
	
	/***
	 * 未登录
	 * @param d
	 * @return
	 */
	public static <T> SRemoteResult<T> initErrorLogin() {
		return new SRemoteResult<T>(SResultCode.PERM_UNLOGIN.getCode(), "请重新登录");
	}

	public static String initSuccessJson(Object d) {
		return GSON_DT.toJson(initSuccess(d));
	}

	public static String initErrorJson(String d) {
		return GSON_DT.toJson(initError(d));
	}

	public static String initErrorLoginJson(String d) {
		return GSON_DT.toJson(initErrorLogin(d));
	}

	public boolean success() {
		if (S_SUCCESS == getS()) {
			return true;
		}
		return false;
	}

	public SRemoteResult() {
		
	}

	public SRemoteResult(T d) {
		this.s = S_SUCCESS;
		this.d = d;
	}

	public SRemoteResult(int s, String msg) {
		this.s = s;
		this.msg = msg;
	}

	public int getS() {
		return s;
	}

	public void setS(int s) {
		this.s = s;
	}

	public T getD() {
		return d;
	}

	public void setD(T d) {
		this.d = d;
	}

	public Integer getTotal() {
		return total;
	}

	public void setTotal(Integer total) {
		this.total = total;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
	
	
}